home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / gmalloc.c < prev    next >
C/C++ Source or Header  |  1993-11-20  |  40KB  |  1,365 lines

  1. /* DO NOT EDIT THIS FILE -- it is automagically generated.  -*- C -*- */
  2.  
  3. #define _MALLOC_INTERNAL
  4.  
  5. /* The malloc headers and source files from the C library follow here.  */
  6.  
  7. /* Declarations for `malloc' and friends.
  8.    Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  9.           Written May 1989 by Mike Haertel.
  10.  
  11. This library is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU Library General Public License as
  13. published by the Free Software Foundation; either version 2 of the
  14. License, or (at your option) any later version.
  15.  
  16. This library is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. Library General Public License for more details.
  20.  
  21. You should have received a copy of the GNU Library General Public
  22. License along with this library; see the file COPYING.LIB.  If
  23. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  24. Cambridge, MA 02139, USA.
  25.  
  26.    The author may be reached (Email) at the address mike@ai.mit.edu,
  27.    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  28.  
  29. #ifndef _MALLOC_H
  30.  
  31. #define _MALLOC_H    1
  32.  
  33. #ifdef _MALLOC_INTERNAL
  34.  
  35. #ifdef    HAVE_CONFIG_H
  36. #include "config.h"
  37. #endif
  38.  
  39. #ifdef WINDOWSNT
  40. #define NOMINMAX
  41. #include <windows.h>    
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include "ntproc_p.h"
  45. #endif /* WINDOWSNT */
  46.  
  47. #if    defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
  48. #include <string.h>
  49. #else
  50. #ifndef memset
  51. #define    memset(s, zero, n)    bzero ((s), (n))
  52. #endif
  53. #ifndef memcpy
  54. #define    memcpy(d, s, n)        bcopy ((s), (d), (n))
  55. #endif
  56. #endif
  57.  
  58. #if    defined(__GNU_LIBRARY__) || defined(__STDC__)
  59. #include <limits.h>
  60. #else
  61. #define    CHAR_BIT    8
  62. #endif
  63.  
  64. #endif    /* _MALLOC_INTERNAL.  */
  65.  
  66.  
  67. #ifdef    __cplusplus
  68. extern "C"
  69. {
  70. #endif
  71.  
  72. #if defined (__cplusplus) || (defined (__STDC__) && __STDC__) || \
  73.     defined(WINDOWSNT)
  74. #undef    __P
  75. #define    __P(args)    args
  76. #undef    __ptr_t
  77. #define    __ptr_t        void *
  78. #else /* Not C++ or ANSI C.  */
  79. #undef    __P
  80. #define    __P(args)    ()
  81. #undef    const
  82. #define    const
  83. #undef    __ptr_t
  84. #define    __ptr_t        char *
  85. #endif /* C++ or ANSI C.  */
  86.  
  87. #ifdef    __STDC__
  88. #include <stddef.h>
  89. #else
  90. #undef    size_t
  91. #define    size_t        unsigned int
  92. #undef    ptrdiff_t
  93. #define    ptrdiff_t    int
  94. #endif
  95.  
  96. #ifndef    NULL
  97. #define    NULL    0
  98. #endif
  99.  
  100.  
  101. #ifndef USE_PROTOTYPES
  102. /* Allocate SIZE bytes of memory.  */
  103. extern __ptr_t malloc __P ((size_t __size));
  104. /* Re-allocate the previously allocated block
  105.    in __ptr_t, making the new block SIZE bytes long.  */
  106. extern __ptr_t realloc __P ((__ptr_t __ptr, size_t __size));
  107. /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0.  */
  108. extern __ptr_t calloc __P ((size_t __nmemb, size_t __size));
  109. /* Free a block allocated by `malloc', `realloc' or `calloc'.  */
  110. extern void free __P ((__ptr_t __ptr));
  111. #endif
  112.  
  113. /* Allocate SIZE bytes allocated to ALIGNMENT bytes.  */
  114. extern __ptr_t memalign __P ((size_t __alignment, size_t __size));
  115.  
  116. /* Allocate SIZE bytes on a page boundary.  */
  117. extern __ptr_t valloc __P ((size_t __size));
  118.  
  119.  
  120. #ifdef _MALLOC_INTERNAL
  121.  
  122. /* The allocator divides the heap into blocks of fixed size; large
  123.    requests receive one or more whole blocks, and small requests
  124.    receive a fragment of a block.  Fragment sizes are powers of two,
  125.    and all fragments of a block are the same size.  When all the
  126.    fragments in a block have been freed, the block itself is freed.  */
  127. #define INT_BIT        (CHAR_BIT * sizeof(int))
  128. #define BLOCKLOG    (INT_BIT > 16 ? 12 : 9)
  129. #define BLOCKSIZE    (1 << BLOCKLOG)
  130. #define BLOCKIFY(SIZE)    (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
  131.  
  132. /* Determine the amount of memory spanned by the initial heap table
  133.    (not an absolute limit).  */
  134. #define HEAP        (INT_BIT > 16 ? 4194304 : 65536)
  135.  
  136. /* Number of contiguous free blocks allowed to build up at the end of
  137.    memory before they will be returned to the system.  */
  138. #define FINAL_FREE_BLOCKS    8
  139.  
  140. /* Data structure giving per-block information.  */
  141. typedef union
  142.   {
  143.     /* Heap information for a busy block.  */
  144.     struct
  145.       {
  146.     /* Zero for a large block, or positive giving the
  147.        logarithm to the base two of the fragment size.  */
  148.     int type;
  149.     union
  150.       {
  151.         struct
  152.           {
  153.         size_t nfree;    /* Free fragments in a fragmented block.  */
  154.         size_t first;    /* First free fragment of the block.  */
  155.           } frag;
  156.         /* Size (in blocks) of a large cluster.  */
  157.         size_t size;
  158.       } info;
  159.       } busy;
  160.     /* Heap information for a free block
  161.        (that may be the first of a free cluster).  */
  162.     struct
  163.       {
  164.     size_t size;        /* Size (in blocks) of a free cluster.  */
  165.     size_t next;        /* Index of next free cluster.  */
  166.     size_t prev;        /* Index of previous free cluster.  */
  167.       } free;
  168.   } malloc_info;
  169.  
  170. /* Pointer to first block of the heap.  */
  171. extern char *_heapbase;
  172.  
  173. /* Table indexed by block number giving per-block information.  */
  174. extern malloc_info *_heapinfo;
  175.  
  176. /* Address to block number and vice versa.  */
  177. #define BLOCK(A)    (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
  178. #define ADDRESS(B)    ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
  179.  
  180. /* Current search index for the heap table.  */
  181. extern size_t _heapindex;
  182.  
  183. /* Limit of valid info table indices.  */
  184. extern size_t _heaplimit;
  185.  
  186. /* Doubly linked lists of free fragments.  */
  187. struct list
  188.   {
  189.     struct list *next;
  190.     struct list *prev;
  191.   };
  192.  
  193. /* Free list headers for each fragment size.  */
  194. extern struct list _fraghead[];
  195.  
  196. /* List of blocks allocated with `memalign' (or `valloc').  */
  197. struct alignlist
  198.   {
  199.     struct alignlist *next;
  200.     __ptr_t aligned;        /* The address that memaligned returned.  */
  201.     __ptr_t exact;        /* The address that malloc returned.  */
  202.   };
  203. extern struct alignlist *_aligned_blocks;
  204.  
  205. /* Instrumentation.  */
  206. extern size_t _chunks_used;
  207. extern size_t _bytes_used;
  208. extern size_t _chunks_free;
  209. extern size_t _bytes_free;
  210.  
  211. /* Internal version of `free' used in `morecore' (malloc.c). */
  212. extern void _free_internal __P ((__ptr_t __ptr));
  213.  
  214. #endif /* _MALLOC_INTERNAL.  */
  215.  
  216. /* Underlying allocation function; successive calls should
  217.    return contiguous pieces of memory.  */
  218. extern __ptr_t (*__morecore) __P ((ptrdiff_t __size));
  219.  
  220. /* Default value of `__morecore'.  */
  221. extern __ptr_t __default_morecore __P ((ptrdiff_t __size));
  222.  
  223. /* If not NULL, this function is called after each time
  224.    `__morecore' is called to increase the data size.  */
  225. extern void (*__after_morecore_hook) __P ((void));
  226.  
  227. /* Nonzero if `malloc' has been called and done its initialization.  */
  228. extern int __malloc_initialized;
  229.  
  230. /* Hooks for debugging versions.  */
  231. extern void (*__free_hook) __P ((__ptr_t __ptr));
  232. extern __ptr_t (*__malloc_hook) __P ((size_t __size));
  233. extern __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, size_t __size));
  234.  
  235. /* Activate a standard collection of debugging hooks.  */
  236. extern int mcheck __P ((void (*__func) __P ((void))));
  237.  
  238. /* Activate a standard collection of tracing hooks.  */
  239. extern void mtrace __P ((void));
  240.  
  241. /* Statistics available to the user.  */
  242. struct mstats
  243.   {
  244.     size_t bytes_total;        /* Total size of the heap. */
  245.     size_t chunks_used;        /* Chunks allocated by the user. */
  246.     size_t bytes_used;        /* Byte total of user-allocated chunks. */
  247.     size_t chunks_free;        /* Chunks in the free list. */
  248.     size_t bytes_free;        /* Byte total of chunks in the free list. */
  249.   };
  250.  
  251. /* Pick up the current statistics. */
  252. extern struct mstats mstats __P ((void));
  253.  
  254. /* Call WARNFUN with a warning message when memory usage is high.  */
  255. extern void memory_warnings __P ((__ptr_t __start,
  256.                   void (*__warnfun) __P ((const char *))));
  257.  
  258.  
  259. /* Relocating allocator.  */
  260.  
  261. /* Allocate SIZE bytes, and store the address in *HANDLEPTR.  */
  262. extern __ptr_t r_alloc __P ((__ptr_t *__handleptr, size_t __size));
  263.  
  264. /* Free the storage allocated in HANDLEPTR.  */
  265. extern void r_alloc_free __P ((__ptr_t *__handleptr));
  266.  
  267. /* Adjust the block at HANDLEPTR to be SIZE bytes long.  */
  268. extern __ptr_t r_re_alloc __P ((__ptr_t *__handleptr, size_t __size));
  269.  
  270.  
  271. #ifdef    __cplusplus
  272. }
  273. #endif
  274.  
  275. #endif /* malloc.h  */
  276. /* Memory allocator `malloc'.
  277.    Copyright 1990, 1991, 1992, 1993 Free Software Foundation
  278.           Written May 1989 by Mike Haertel.
  279.  
  280. This library is free software; you can redistribute it and/or
  281. modify it under the terms of the GNU Library General Public License as
  282. published by the Free Software Foundation; either version 2 of the
  283. License, or (at your option) any later version.
  284.  
  285. This library is distributed in the hope that it will be useful,
  286. but WITHOUT ANY WARRANTY; without even the implied warranty of
  287. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  288. Library General Public License for more details.
  289.  
  290. You should have received a copy of the GNU Library General Public
  291. License along with this library; see the file COPYING.LIB.  If
  292. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  293. Cambridge, MA 02139, USA.
  294.  
  295.    The author may be reached (Email) at the address mike@ai.mit.edu,
  296.    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  297.  
  298. #ifndef    _MALLOC_INTERNAL
  299. #define _MALLOC_INTERNAL
  300. #include <malloc.h>
  301. #endif
  302.  
  303. /* How to really get more memory.  */
  304. __ptr_t (*__morecore) __P ((ptrdiff_t __size)) = __default_morecore;
  305.  
  306. /* Debugging hook for `malloc'.  */
  307. __ptr_t (*__malloc_hook) __P ((size_t __size));
  308.  
  309. /* Pointer to the base of the first block.  */
  310. char *_heapbase;
  311.  
  312. /* Block information table.  Allocated with align/__free (not malloc/free).  */
  313. malloc_info *_heapinfo;
  314.  
  315. /* Number of info entries.  */
  316. static size_t heapsize;
  317.  
  318. /* Search index in the info table.  */
  319. size_t _heapindex;
  320.  
  321. /* Limit of valid info table indices.  */
  322. size_t _heaplimit;
  323.  
  324. /* Free lists for each fragment size.  */
  325. struct list _fraghead[BLOCKLOG];
  326.  
  327. /* Instrumentation.  */
  328. size_t _chunks_used;
  329. size_t _bytes_used;
  330. size_t _chunks_free;
  331. size_t _bytes_free;
  332.  
  333. /* Are you experienced?  */
  334. int __malloc_initialized;
  335.  
  336. void (*__after_morecore_hook) __P ((void));
  337.  
  338. /* Aligned allocation.  */
  339. static __ptr_t align __P ((size_t));
  340. static __ptr_t
  341. align (size)
  342.      size_t size;
  343. {
  344.   __ptr_t result;
  345.   unsigned long int adj;
  346.  
  347.   result = (*__morecore) (size);
  348.   adj = (unsigned long int) ((unsigned long int) ((char *) result -
  349.                         (char *) NULL)) % BLOCKSIZE;
  350.   if (adj != 0)
  351.     {
  352.       adj = BLOCKSIZE - adj;
  353.       (void) (*__morecore) (adj);
  354.       result = (char *) result + adj;
  355.     }
  356.  
  357.   if (__after_morecore_hook)
  358.     (*__after_morecore_hook) ();
  359.  
  360.   return result;
  361. }
  362.  
  363. /* Set everything up and remember that we have.  */
  364. static int initialize __P ((void));
  365. static int
  366. initialize ()
  367. {
  368.   heapsize = HEAP / BLOCKSIZE;
  369.   _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info));
  370.   if (_heapinfo == NULL)
  371.     return 0;
  372.   memset (_heapinfo, 0, heapsize * sizeof (malloc_info));
  373.   _heapinfo[0].free.size = 0;
  374.   _heapinfo[0].free.next = _heapinfo[0].free.prev = 0;
  375.   _heapindex = 0;
  376.   _heapbase = (char *) _heapinfo;
  377.   __malloc_initialized = 1;
  378.   return 1;
  379. }
  380.  
  381. /* Get neatly aligned memory, initializing or
  382.    growing the heap info table as necessary. */
  383. static __ptr_t morecore __P ((size_t));
  384. static __ptr_t
  385. morecore (size)
  386.      size_t size;
  387. {
  388.   __ptr_t result;
  389.   malloc_info *newinfo, *oldinfo;
  390.   size_t newsize;
  391.  
  392.   result = align (size);
  393.   if (result == NULL)
  394.     return NULL;
  395.  
  396.   /* Check if we need to grow the info table.  */
  397.   if ((size_t) BLOCK ((char *) result + size) > heapsize)
  398.     {
  399.       newsize = heapsize;
  400.       while ((size_t) BLOCK ((char *) result + size) > newsize)
  401.     newsize *= 2;
  402.       newinfo = (malloc_info *) align (newsize * sizeof (malloc_info));
  403.       if (newinfo == NULL)
  404.     {
  405.       (*__morecore) (-(ptrdiff_t)size);
  406.       return NULL;
  407.     }
  408.       memset (newinfo, 0, newsize * sizeof (malloc_info));
  409.       memcpy (newinfo, _heapinfo, heapsize * sizeof (malloc_info));
  410.       oldinfo = _heapinfo;
  411.       newinfo[BLOCK (oldinfo)].busy.type = 0;
  412.       newinfo[BLOCK (oldinfo)].busy.info.size
  413.     = BLOCKIFY (heapsize * sizeof (malloc_info));
  414.       _heapinfo = newinfo;
  415.       _free_internal (oldinfo);
  416.       heapsize = newsize;
  417.     }
  418.  
  419.   _heaplimit = BLOCK ((char *) result + size);
  420.   return result;
  421. }
  422.  
  423. /* Allocate memory from the heap.  */
  424. __ptr_t
  425. #ifdef WINDOWSNT
  426. _CRTAPI1
  427. #endif
  428. malloc (size)
  429.      size_t size;
  430. {
  431.   __ptr_t result;
  432.   size_t block, blocks, lastblocks, start;
  433.   register size_t i;
  434.   struct list *next;
  435.  
  436.   /* ANSI C allows `malloc (0)' to either return NULL, or to return a
  437.      valid address you can realloc and free (though not dereference).
  438.  
  439.      It turns out that some extant code (sunrpc, at least Ultrix's version)
  440.      expects `malloc (0)' to return non-NULL and breaks otherwise.
  441.      Be compatible.  */
  442.  
  443. #if    0
  444.   if (size == 0)
  445.     return NULL;
  446. #endif
  447.  
  448.   if (__malloc_hook != NULL)
  449.     return (*__malloc_hook) (size);
  450.  
  451.   if (!__malloc_initialized)
  452.     if (!initialize ())
  453.       return NULL;
  454.  
  455.   if (size < sizeof (struct list))
  456.       size = sizeof (struct list);
  457.  
  458.   /* Determine the allocation policy based on the request size.  */
  459.   if (size <= BLOCKSIZE / 2)
  460.     {
  461.       /* Small allocation to receive a fragment of a block.
  462.      Determine the logarithm to base two of the fragment size. */
  463.       register size_t log = 1;
  464.       --size;
  465.       while ((size /= 2) != 0)
  466.     ++log;
  467.  
  468.       /* Look in the fragment lists for a
  469.      free fragment of the desired size. */
  470.       next = _fraghead[log].next;
  471.       if (next != NULL)
  472.     {
  473.       /* There are free fragments of this size.
  474.          Pop a fragment out of the fragment list and return it.
  475.          Update the block's nfree and first counters. */
  476.       result = (__ptr_t) next;
  477.       next->prev->next = next->next;
  478.       if (next->next != NULL)
  479.         next->next->prev = next->prev;
  480.       block = BLOCK (result);
  481.       if (--_heapinfo[block].busy.info.frag.nfree != 0)
  482.         _heapinfo[block].busy.info.frag.first = (unsigned long int)
  483.           ((unsigned long int) ((char *) next->next - (char *) NULL)
  484.            % BLOCKSIZE) >> log;
  485.  
  486.       /* Update the statistics.  */
  487.       ++_chunks_used;
  488.       _bytes_used += 1 << log;
  489.       --_chunks_free;
  490.       _bytes_free -= 1 << log;
  491.     }
  492.       else
  493.     {
  494.       /* No free fragments of the desired size, so get a new block
  495.          and break it into fragments, returning the first.  */
  496.       result = malloc (BLOCKSIZE);
  497.       if (result == NULL)
  498.         return NULL;
  499.  
  500.       /* Link all fragments but the first into the free list.  */
  501.       for (i = 1; i < (size_t) (BLOCKSIZE >> log); ++i)
  502.         {
  503.           next = (struct list *) ((char *) result + (i << log));
  504.           next->next = _fraghead[log].next;
  505.           next->prev = &_fraghead[log];
  506.           next->prev->next = next;
  507.           if (next->next != NULL)
  508.         next->next->prev = next;
  509.         }
  510.  
  511.       /* Initialize the nfree and first counters for this block.  */
  512.       block = BLOCK (result);
  513.       _heapinfo[block].busy.type = log;
  514.       _heapinfo[block].busy.info.frag.nfree = i - 1;
  515.       _heapinfo[block].busy.info.frag.first = i - 1;
  516.  
  517.       _chunks_free += (BLOCKSIZE >> log) - 1;
  518.       _bytes_free += BLOCKSIZE - (1 << log);
  519.       _bytes_used -= BLOCKSIZE - (1 << log);
  520.     }
  521.     }
  522.   else
  523.     {
  524.       /* Large allocation to receive one or more blocks.
  525.      Search the free list in a circle starting at the last place visited.
  526.      If we loop completely around without finding a large enough
  527.      space we will have to get more memory from the system.  */
  528.       blocks = BLOCKIFY (size);
  529.       start = block = _heapindex;
  530.       while (_heapinfo[block].free.size < blocks)
  531.     {
  532.       block = _heapinfo[block].free.next;
  533.       if (block == start)
  534.         {
  535.           /* Need to get more from the system.  Check to see if
  536.          the new core will be contiguous with the final free
  537.          block; if so we don't need to get as much.  */
  538.           block = _heapinfo[0].free.prev;
  539.           lastblocks = _heapinfo[block].free.size;
  540.           if (_heaplimit != 0 && block + lastblocks == _heaplimit &&
  541.           (*__morecore) (0) == ADDRESS (block + lastblocks) &&
  542.           (morecore ((blocks - lastblocks) * BLOCKSIZE)) != NULL)
  543.         {
  544.           _heapinfo[block].free.size = blocks;
  545.           _bytes_free += (blocks - lastblocks) * BLOCKSIZE;
  546.           continue;
  547.         }
  548.           result = morecore (blocks * BLOCKSIZE);
  549.           if (result == NULL)
  550.         return NULL;
  551.           block = BLOCK (result);
  552.           _heapinfo[block].busy.type = 0;
  553.           _heapinfo[block].busy.info.size = blocks;
  554.           ++_chunks_used;
  555.           _bytes_used += blocks * BLOCKSIZE;
  556.           return result;
  557.         }
  558.     }
  559.  
  560.       /* At this point we have found a suitable free list entry.
  561.      Figure out how to remove what we need from the list. */
  562.       result = ADDRESS (block);
  563.       if (_heapinfo[block].free.size > blocks)
  564.     {
  565.       /* The block we found has a bit left over,
  566.          so relink the tail end back into the free list. */
  567.       _heapinfo[block + blocks].free.size
  568.         = _heapinfo[block].free.size - blocks;
  569.       _heapinfo[block + blocks].free.next
  570.         = _heapinfo[block].free.next;
  571.       _heapinfo[block + blocks].free.prev
  572.         = _heapinfo[block].free.prev;
  573.       _heapinfo[_heapinfo[block].free.prev].free.next
  574.         = _heapinfo[_heapinfo[block].free.next].free.prev
  575.         = _heapindex = block + blocks;
  576.     }
  577.       else
  578.     {
  579.       /* The block exactly matches our requirements,
  580.          so just remove it from the list. */
  581.       _heapinfo[_heapinfo[block].free.next].free.prev
  582.         = _heapinfo[block].free.prev;
  583.       _heapinfo[_heapinfo[block].free.prev].free.next
  584.         = _heapindex = _heapinfo[block].free.next;
  585.       --_chunks_free;
  586.     }
  587.  
  588.       _heapinfo[block].busy.type = 0;
  589.       _heapinfo[block].busy.info.size = blocks;
  590.       ++_chunks_used;
  591.       _bytes_used += blocks * BLOCKSIZE;
  592.       _bytes_free -= blocks * BLOCKSIZE;
  593.     }
  594.  
  595.   return result;
  596. }
  597.  
  598. #ifndef _LIBC
  599.  
  600. /* On some ANSI C systems, some libc functions call _malloc, _free
  601.    and _realloc.  Make them use the GNU functions.  */
  602.  
  603. __ptr_t
  604. _malloc (size)
  605.      size_t size;
  606. {
  607.   return malloc (size);
  608. }
  609.  
  610. void
  611. _free (ptr)
  612.      __ptr_t ptr;
  613. {
  614.   free (ptr);
  615. }
  616.  
  617. __ptr_t
  618. _realloc (ptr, size)
  619.      __ptr_t ptr;
  620.      size_t size;
  621. {
  622.   return realloc (ptr, size);
  623. }
  624.  
  625. #endif
  626. /* Free a block of memory allocated by `malloc'.
  627.    Copyright 1990, 1991, 1992 Free Software Foundation
  628.           Written May 1989 by Mike Haertel.
  629.  
  630. This library is free software; you can redistribute it and/or
  631. modify it under the terms of the GNU Library General Public License as
  632. published by the Free Software Foundation; either version 2 of the
  633. License, or (at your option) any later version.
  634.  
  635. This library is distributed in the hope that it will be useful,
  636. but WITHOUT ANY WARRANTY; without even the implied warranty of
  637. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  638. Library General Public License for more details.
  639.  
  640. You should have received a copy of the GNU Library General Public
  641. License along with this library; see the file COPYING.LIB.  If
  642. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  643. Cambridge, MA 02139, USA.
  644.  
  645.    The author may be reached (Email) at the address mike@ai.mit.edu,
  646.    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  647.  
  648. #ifndef    _MALLOC_INTERNAL
  649. #define _MALLOC_INTERNAL
  650. #include <malloc.h>
  651. #endif
  652.  
  653. /* Debugging hook for free.  */
  654. void (*__free_hook) __P ((__ptr_t __ptr));
  655.  
  656. /* List of blocks allocated by memalign.  */
  657. struct alignlist *_aligned_blocks = NULL;
  658.  
  659. /* Return memory to the heap.
  660.    Like `free' but don't call a __free_hook if there is one.  */
  661. void
  662. _free_internal (ptr)
  663.      __ptr_t ptr;
  664. {
  665.   int type;
  666.   size_t block, blocks;
  667.   register size_t i;
  668.   struct list *prev, *next;
  669.  
  670.   block = BLOCK (ptr);
  671.  
  672.   type = _heapinfo[block].busy.type;
  673.   switch (type)
  674.     {
  675.     case 0:
  676.       /* Get as many statistics as early as we can.  */
  677.       --_chunks_used;
  678.       _bytes_used -= _heapinfo[block].busy.info.size * BLOCKSIZE;
  679.       _bytes_free += _heapinfo[block].busy.info.size * BLOCKSIZE;
  680.  
  681.       /* Find the free cluster previous to this one in the free list.
  682.      Start searching at the last block referenced; this may benefit
  683.      programs with locality of allocation.  */
  684.       i = _heapindex;
  685.       if (i > block)
  686.     while (i > block)
  687.       i = _heapinfo[i].free.prev;
  688.       else
  689.     {
  690.       do
  691.         i = _heapinfo[i].free.next;
  692.       while (i > 0 && i < block);
  693.       i = _heapinfo[i].free.prev;
  694.     }
  695.  
  696.       /* Determine how to link this block into the free list.  */
  697.       if (block == i + _heapinfo[i].free.size)
  698.     {
  699.       /* Coalesce this block with its predecessor.  */
  700.       _heapinfo[i].free.size += _heapinfo[block].busy.info.size;
  701.       block = i;
  702.     }
  703.       else
  704.     {
  705.       /* Really link this block back into the free list.  */
  706.       _heapinfo[block].free.size = _heapinfo[block].busy.info.size;
  707.       _heapinfo[block].free.next = _heapinfo[i].free.next;
  708.       _heapinfo[block].free.prev = i;
  709.       _heapinfo[i].free.next = block;
  710.       _heapinfo[_heapinfo[block].free.next].free.prev = block;
  711.       ++_chunks_free;
  712.     }
  713.  
  714.       /* Now that the block is linked in, see if we can coalesce it
  715.      with its successor (by deleting its successor from the list
  716.      and adding in its size).  */
  717.       if (block + _heapinfo[block].free.size == _heapinfo[block].free.next)
  718.     {
  719.       _heapinfo[block].free.size
  720.         += _heapinfo[_heapinfo[block].free.next].free.size;
  721.       _heapinfo[block].free.next
  722.         = _heapinfo[_heapinfo[block].free.next].free.next;
  723.       _heapinfo[_heapinfo[block].free.next].free.prev = block;
  724.       --_chunks_free;
  725.     }
  726.  
  727.       /* Now see if we can return stuff to the system.  */
  728.       blocks = _heapinfo[block].free.size;
  729.       if (blocks >= FINAL_FREE_BLOCKS && block + blocks == _heaplimit
  730.       && (*__morecore) (0) == ADDRESS (block + blocks))
  731.     {
  732.       register size_t bytes = blocks * BLOCKSIZE;
  733.       _heaplimit -= blocks;
  734.       (*__morecore) (-(ptrdiff_t)bytes);
  735.       _heapinfo[_heapinfo[block].free.prev].free.next
  736.         = _heapinfo[block].free.next;
  737.       _heapinfo[_heapinfo[block].free.next].free.prev
  738.         = _heapinfo[block].free.prev;
  739.       block = _heapinfo[block].free.prev;
  740.       --_chunks_free;
  741.       _bytes_free -= bytes;
  742.     }
  743.  
  744.       /* Set the next search to begin at this block.  */
  745.       _heapindex = block;
  746.       break;
  747.  
  748.     default:
  749.       /* Do some of the statistics.  */
  750.       --_chunks_used;
  751.       _bytes_used -= 1 << type;
  752.       ++_chunks_free;
  753.       _bytes_free += 1 << type;
  754.  
  755.       /* Get the address of the first free fragment in this block.  */
  756.       prev = (struct list *) ((char *) ADDRESS (block) +
  757.                (_heapinfo[block].busy.info.frag.first << type));
  758.  
  759.       if (_heapinfo[block].busy.info.frag.nfree ==
  760.           (size_t)((BLOCKSIZE >> type) - 1))
  761.     {
  762.       /* If all fragments of this block are free, remove them
  763.          from the fragment list and free the whole block.  */
  764.       next = prev;
  765.       for (i = 1; i < (size_t) (BLOCKSIZE >> type); ++i)
  766.         next = next->next;
  767.       prev->prev->next = next;
  768.       if (next != NULL)
  769.         next->prev = prev->prev;
  770.       _heapinfo[block].busy.type = 0;
  771.       _heapinfo[block].busy.info.size = 1;
  772.  
  773.       /* Keep the statistics accurate.  */
  774.       ++_chunks_used;
  775.       _bytes_used += BLOCKSIZE;
  776.       _chunks_free -= BLOCKSIZE >> type;
  777.       _bytes_free -= BLOCKSIZE;
  778.  
  779.       free (ADDRESS (block));
  780.     }
  781.       else if (_heapinfo[block].busy.info.frag.nfree != 0)
  782.     {
  783.       /* If some fragments of this block are free, link this
  784.          fragment into the fragment list after the first free
  785.          fragment of this block. */
  786.       next = (struct list *) ptr;
  787.       next->next = prev->next;
  788.       next->prev = prev;
  789.       prev->next = next;
  790.       if (next->next != NULL)
  791.         next->next->prev = next;
  792.       ++_heapinfo[block].busy.info.frag.nfree;
  793.     }
  794.       else
  795.     {
  796.       /* No fragments of this block are free, so link this
  797.          fragment into the fragment list and announce that
  798.          it is the first free fragment of this block. */
  799.       prev = (struct list *) ptr;
  800.       _heapinfo[block].busy.info.frag.nfree = 1;
  801.       _heapinfo[block].busy.info.frag.first = (unsigned long int)
  802.         ((unsigned long int) ((char *) ptr - (char *) NULL)
  803.          % BLOCKSIZE >> type);
  804.       prev->next = _fraghead[type].next;
  805.       prev->prev = &_fraghead[type];
  806.       prev->prev->next = prev;
  807.       if (prev->next != NULL)
  808.         prev->next->prev = prev;
  809.     }
  810.       break;
  811.     }
  812. }
  813.  
  814. /* Return memory to the heap.  */
  815. void
  816. #ifdef WINDOWSNT
  817. _CRTAPI1
  818. #endif
  819. free (ptr)
  820.      __ptr_t ptr;
  821. {
  822.   register struct alignlist *l;
  823.  
  824.   if (ptr == NULL)
  825.     return;
  826.  
  827.   for (l = _aligned_blocks; l != NULL; l = l->next)
  828.     if (l->aligned == ptr)
  829.       {
  830.     l->aligned = NULL;    /* Mark the slot in the list as free.  */
  831.     ptr = l->exact;
  832.     break;
  833.       }
  834.  
  835.   if (__free_hook != NULL)
  836.     (*__free_hook) (ptr);
  837.   else
  838.     _free_internal (ptr);
  839. }
  840. /* Copyright (C) 1991, 1993 Free Software Foundation, Inc.
  841. This file is part of the GNU C Library.
  842.  
  843. The GNU C Library is free software; you can redistribute it and/or
  844. modify it under the terms of the GNU Library General Public License as
  845. published by the Free Software Foundation; either version 2 of the
  846. License, or (at your option) any later version.
  847.  
  848. The GNU C Library is distributed in the hope that it will be useful,
  849. but WITHOUT ANY WARRANTY; without even the implied warranty of
  850. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  851. Library General Public License for more details.
  852.  
  853. You should have received a copy of the GNU Library General Public
  854. License along with the GNU C Library; see the file COPYING.LIB.  If
  855. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  856. Cambridge, MA 02139, USA.  */
  857.  
  858. #ifndef    _MALLOC_INTERNAL
  859. #define _MALLOC_INTERNAL
  860. #include <malloc.h>
  861. #endif
  862.  
  863. #undef    cfree
  864.  
  865. #ifdef _LIBC
  866.  
  867. #include <ansidecl.h>
  868. #include <gnu-stabs.h>
  869.  
  870. function_alias(cfree, free, void, (ptr),
  871.            DEFUN(cfree, (ptr), PTR ptr))
  872.  
  873. #else
  874.  
  875. void
  876. cfree (ptr)
  877.      __ptr_t ptr;
  878. {
  879.   free (ptr);
  880. }
  881.  
  882. #endif
  883. /* Change the size of a block allocated by `malloc'.
  884.    Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  885.              Written May 1989 by Mike Haertel.
  886.  
  887. This library is free software; you can redistribute it and/or
  888. modify it under the terms of the GNU Library General Public License as
  889. published by the Free Software Foundation; either version 2 of the
  890. License, or (at your option) any later version.
  891.  
  892. This library is distributed in the hope that it will be useful,
  893. but WITHOUT ANY WARRANTY; without even the implied warranty of
  894. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  895. Library General Public License for more details.
  896.  
  897. You should have received a copy of the GNU Library General Public
  898. License along with this library; see the file COPYING.LIB.  If
  899. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  900. Cambridge, MA 02139, USA.
  901.  
  902.    The author may be reached (Email) at the address mike@ai.mit.edu,
  903.    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  904.  
  905. #ifndef    _MALLOC_INTERNAL
  906. #define _MALLOC_INTERNAL
  907. #include <malloc.h>
  908. #endif
  909.  
  910. #if    !defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG)
  911.  
  912. /* Snarfed directly from Emacs src/dispnew.c:
  913.    XXX Should use system bcopy if it handles overlap.  */
  914.  
  915. /* Like bcopy except never gets confused by overlap.  */
  916.  
  917. static void
  918. safe_bcopy (from, to, size)
  919.      char *from, *to;
  920.      int size;
  921. {
  922.   if (size <= 0 || from == to)
  923.     return;
  924.  
  925.   /* If the source and destination don't overlap, then bcopy can
  926.      handle it.  If they do overlap, but the destination is lower in
  927.      memory than the source, we'll assume bcopy can handle that.  */
  928.   if (to < from || from + size <= to)
  929.     bcopy (from, to, size);
  930.  
  931.   /* Otherwise, we'll copy from the end.  */
  932.   else
  933.     {
  934.       register char *endf = from + size;
  935.       register char *endt = to + size;
  936.  
  937.       /* If TO - FROM is large, then we should break the copy into
  938.      nonoverlapping chunks of TO - FROM bytes each.  However, if
  939.      TO - FROM is small, then the bcopy function call overhead
  940.      makes this not worth it.  The crossover point could be about
  941.      anywhere.  Since I don't think the obvious copy loop is too
  942.      bad, I'm trying to err in its favor.  */
  943.       if (to - from < 64)
  944.     {
  945.       do
  946.         *--endt = *--endf;
  947.       while (endf != from);
  948.     }
  949.       else
  950.     {
  951.       for (;;)
  952.         {
  953.           endt -= (to - from);
  954.           endf -= (to - from);
  955.  
  956.           if (endt < to)
  957.         break;
  958.  
  959.           bcopy (endf, endt, to - from);
  960.         }
  961.  
  962.       /* If SIZE wasn't a multiple of TO - FROM, there will be a
  963.          little left over.  The amount left over is
  964.          (endt + (to - from)) - to, which is endt - from.  */
  965.       bcopy (from, to, endt - from);
  966.     }
  967.     }
  968. }     
  969.  
  970. #define memmove(to, from, size) safe_bcopy ((from), (to), (size))
  971.  
  972. #endif
  973.  
  974.  
  975. #ifndef min
  976. #define min(A, B) ((A) < (B) ? (A) : (B))
  977. #endif
  978.  
  979. /* Debugging hook for realloc.  */
  980. __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, size_t __size));
  981.  
  982. /* Resize the given region to the new size, returning a pointer
  983.    to the (possibly moved) region.  This is optimized for speed;
  984.    some benchmarks seem to indicate that greater compactness is
  985.    achieved by unconditionally allocating and copying to a
  986.    new region.  This module has incestuous knowledge of the
  987.    internals of both free and malloc. */
  988. __ptr_t
  989. #ifdef WINDOWSNT
  990. _CRTAPI1
  991. #endif
  992. realloc (ptr, size)
  993.      __ptr_t ptr;
  994.      size_t size;
  995. {
  996.   __ptr_t result;
  997.   int type;
  998.   size_t block, blocks, oldlimit;
  999.  
  1000.   if (size == 0)
  1001.     {
  1002.       free (ptr);
  1003.       return malloc (0);
  1004.     }
  1005.   else if (ptr == NULL)
  1006.     return malloc (size);
  1007.  
  1008.   if (__realloc_hook != NULL)
  1009.     return (*__realloc_hook) (ptr, size);
  1010.  
  1011.   block = BLOCK (ptr);
  1012.  
  1013.   type = _heapinfo[block].busy.type;
  1014.   switch (type)
  1015.     {
  1016.     case 0:
  1017.       /* Maybe reallocate a large block to a small fragment.  */
  1018.       if (size <= BLOCKSIZE / 2)
  1019.     {
  1020.       result = malloc (size);
  1021.       if (result != NULL)
  1022.         {
  1023.           memcpy (result, ptr, size);
  1024.           free (ptr);
  1025.           return result;
  1026.         }
  1027.     }
  1028.  
  1029.       /* The new size is a large allocation as well;
  1030.      see if we can hold it in place. */
  1031.       blocks = BLOCKIFY (size);
  1032.       if (blocks < _heapinfo[block].busy.info.size)
  1033.     {
  1034.       /* The new size is smaller; return
  1035.          excess memory to the free list. */
  1036.       _heapinfo[block + blocks].busy.type = 0;
  1037.       _heapinfo[block + blocks].busy.info.size
  1038.         = _heapinfo[block].busy.info.size - blocks;
  1039.       _heapinfo[block].busy.info.size = blocks;
  1040.       free (ADDRESS (block + blocks));
  1041.       result = ptr;
  1042.     }
  1043.       else if (blocks == _heapinfo[block].busy.info.size)
  1044.     /* No size change necessary.  */
  1045.     result = ptr;
  1046.       else
  1047.     {
  1048.       /* Won't fit, so allocate a new region that will.
  1049.          Free the old region first in case there is sufficient
  1050.          adjacent free space to grow without moving. */
  1051.       blocks = _heapinfo[block].busy.info.size;
  1052.       /* Prevent free from actually returning memory to the system.  */
  1053.       oldlimit = _heaplimit;
  1054.       _heaplimit = 0;
  1055.       free (ptr);
  1056.       _heaplimit = oldlimit;
  1057.       result = malloc (size);
  1058.       if (result == NULL)
  1059.         {
  1060.           /* Now we're really in trouble.  We have to unfree
  1061.          the thing we just freed.  Unfortunately it might
  1062.          have been coalesced with its neighbors.  */
  1063.           if (_heapindex == block)
  1064.             (void) malloc (blocks * BLOCKSIZE);
  1065.           else
  1066.         {
  1067.           __ptr_t previous = malloc ((block - _heapindex) * BLOCKSIZE);
  1068.           (void) malloc (blocks * BLOCKSIZE);
  1069.           free (previous);
  1070.         }
  1071.           return NULL;
  1072.         }
  1073.       if (ptr != result)
  1074.         memmove (result, ptr, blocks * BLOCKSIZE);
  1075.     }
  1076.       break;
  1077.  
  1078.     default:
  1079.       /* Old size is a fragment; type is logarithm
  1080.      to base two of the fragment size.  */
  1081.       if (size > (size_t) (1 << (type - 1)) && size <= (size_t) (1 << type))
  1082.     /* The new size is the same kind of fragment.  */
  1083.     result = ptr;
  1084.       else
  1085.     {
  1086.       /* The new size is different; allocate a new space,
  1087.          and copy the lesser of the new size and the old. */
  1088.       result = malloc (size);
  1089.       if (result == NULL)
  1090.         return NULL;
  1091.       memcpy (result, ptr, min (size, (size_t) 1 << type));
  1092.       free (ptr);
  1093.     }
  1094.       break;
  1095.     }
  1096.  
  1097.   return result;
  1098. }
  1099. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  1100.  
  1101. This library is free software; you can redistribute it and/or
  1102. modify it under the terms of the GNU Library General Public License as
  1103. published by the Free Software Foundation; either version 2 of the
  1104. License, or (at your option) any later version.
  1105.  
  1106. This library is distributed in the hope that it will be useful,
  1107. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1108. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  1109. Library General Public License for more details.
  1110.  
  1111. You should have received a copy of the GNU Library General Public
  1112. License along with this library; see the file COPYING.LIB.  If
  1113. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  1114. Cambridge, MA 02139, USA.
  1115.  
  1116.    The author may be reached (Email) at the address mike@ai.mit.edu,
  1117.    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  1118.  
  1119. #ifndef    _MALLOC_INTERNAL
  1120. #define    _MALLOC_INTERNAL
  1121. #include <malloc.h>
  1122. #endif
  1123.  
  1124. /* Allocate an array of NMEMB elements each SIZE bytes long.
  1125.    The entire array is initialized to zeros.  */
  1126. __ptr_t
  1127. #ifdef WINDOWSNT
  1128. _CRTAPI1
  1129. #endif
  1130. calloc (nmemb, size)
  1131.      register size_t nmemb;
  1132.      register size_t size;
  1133. {
  1134.   register __ptr_t result = malloc (nmemb * size);
  1135.  
  1136.   if (result != NULL)
  1137.     (void) memset (result, 0, nmemb * size);
  1138.  
  1139.   return result;
  1140. }
  1141. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  1142. This file is part of the GNU C Library.
  1143.  
  1144. The GNU C Library is free software; you can redistribute it and/or modify
  1145. it under the terms of the GNU General Public License as published by
  1146. the Free Software Foundation; either version 2, or (at your option)
  1147. any later version.
  1148.  
  1149. The GNU C Library is distributed in the hope that it will be useful,
  1150. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1151. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1152. GNU General Public License for more details.
  1153.  
  1154. You should have received a copy of the GNU General Public License
  1155. along with the GNU C Library; see the file COPYING.  If not, write to
  1156. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  1157.  
  1158. #ifndef    _MALLOC_INTERNAL
  1159. #define    _MALLOC_INTERNAL
  1160. #include <malloc.h>
  1161. #endif
  1162.  
  1163. #ifndef    __GNU_LIBRARY__
  1164. #define    __sbrk    sbrk
  1165. #endif
  1166.  
  1167. extern __ptr_t __sbrk __P ((int increment));
  1168.  
  1169. #ifndef NULL
  1170. #define NULL 0
  1171. #endif
  1172.  
  1173. #ifdef WINDOWSNT
  1174. /*
  1175.  * We use these in __default_morecore to emulate a contiguous
  1176.  * heap for emacs (sbrk/brk).  (voelker)
  1177.  */
  1178. char *DataRegionBase;
  1179. char *DataRegionEnd;
  1180. DWORD DataRegionSize;
  1181.  
  1182. #define WIN_NT_PAGE_SIZE    4096
  1183. #define MAX_DATA_ALLOCATED    (2 * (HEAP))
  1184. #define FAKE_DATA_REGION_BASE    ((char *)0x00100000)
  1185.  
  1186. #endif /* WINDOWSNT */
  1187.  
  1188. /* Allocate INCREMENT more bytes of data space,
  1189.    and return the start of data space, or NULL on errors.
  1190.    If INCREMENT is negative, shrink data space.  */
  1191. __ptr_t
  1192. __default_morecore (increment)
  1193.      ptrdiff_t increment;
  1194. {
  1195. #ifdef WINDOWSNT
  1196.   __ptr_t result;
  1197.   int size = (int) increment;
  1198.  
  1199.   DebPrint(("morecore(%d), currently using %d\n", size,
  1200.             DataRegionEnd - DataRegionBase));
  1201.   
  1202.   if (!DataRegionBase)
  1203.   {
  1204.       DataRegionBase = VirtualAlloc(FAKE_DATA_REGION_BASE,
  1205.                     MAX_DATA_ALLOCATED,
  1206.                     MEM_RESERVE,
  1207.                     PAGE_NOACCESS);
  1208.       if (!DataRegionBase)
  1209.           return NULL;
  1210.  
  1211.       /*
  1212.        * We must ensure that the addresses don't use the upper 8 bits
  1213.        * since the Lisp type goes there
  1214.        * Yucko.
  1215.        */
  1216.       if (((DWORD)DataRegionBase & 0xFF000000) != 0)
  1217.           return NULL;
  1218.       
  1219.       DataRegionEnd = DataRegionBase;
  1220.       DataRegionSize = MAX_DATA_ALLOCATED;
  1221.   }
  1222.  
  1223.   result = DataRegionEnd;
  1224.   if (size < 0)
  1225.   {
  1226.       size = -size;
  1227.       if ((size < WIN_NT_PAGE_SIZE) || (size % WIN_NT_PAGE_SIZE != 0))
  1228.           return NULL;
  1229.       if (DataRegionEnd - size < DataRegionBase)
  1230.           return NULL;
  1231.       
  1232.       if (!VirtualFree(DataRegionEnd - size, size, MEM_DECOMMIT))
  1233.           return NULL;
  1234.       DataRegionEnd -= size;
  1235.   }
  1236.   else if (size > 0)
  1237.   {
  1238.       if ((size < WIN_NT_PAGE_SIZE) || (size % WIN_NT_PAGE_SIZE != 0))
  1239.           return NULL;
  1240.       if ((DataRegionEnd + size) >
  1241.           (DataRegionBase + MAX_DATA_ALLOCATED))
  1242.           return NULL;
  1243.  
  1244.       if (VirtualAlloc(DataRegionEnd, size, MEM_COMMIT,
  1245.                        PAGE_READWRITE) == NULL)
  1246.           return NULL;
  1247.       DataRegionEnd += size;
  1248.   }
  1249.   
  1250.   return result;
  1251.  
  1252. #else  /* !WINDOWSNT */
  1253.   __ptr_t result = __sbrk ((int) increment);
  1254.  
  1255.   if (result == (__ptr_t) -1)
  1256.     return NULL;
  1257.   return result;
  1258. #endif /* !WINDOWSNT */
  1259. }
  1260. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  1261.  
  1262. This library is free software; you can redistribute it and/or
  1263. modify it under the terms of the GNU Library General Public License as
  1264. published by the Free Software Foundation; either version 2 of the
  1265. License, or (at your option) any later version.
  1266.  
  1267. This library is distributed in the hope that it will be useful,
  1268. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1269. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  1270. Library General Public License for more details.
  1271.  
  1272. You should have received a copy of the GNU Library General Public
  1273. License along with this library; see the file COPYING.LIB.  If
  1274. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  1275. Cambridge, MA 02139, USA.  */
  1276.  
  1277. #ifndef    _MALLOC_INTERNAL
  1278. #define _MALLOC_INTERNAL
  1279. #include <malloc.h>
  1280. #endif
  1281.  
  1282. __ptr_t
  1283. memalign (alignment, size)
  1284.      size_t alignment;
  1285.      size_t size;
  1286. {
  1287.   __ptr_t result;
  1288.   unsigned long int adj;
  1289.  
  1290.   size = ((size + alignment - 1) / alignment) * alignment;
  1291.  
  1292.   result = malloc (size);
  1293.   if (result == NULL)
  1294.     return NULL;
  1295.   adj = (unsigned long int) ((unsigned long int) ((char *) result -
  1296.                         (char *) NULL)) % alignment;
  1297.   if (adj != 0)
  1298.     {
  1299.       struct alignlist *l;
  1300.       for (l = _aligned_blocks; l != NULL; l = l->next)
  1301.     if (l->aligned == NULL)
  1302.       /* This slot is free.  Use it.  */
  1303.       break;
  1304.       if (l == NULL)
  1305.     {
  1306.       l = (struct alignlist *) malloc (sizeof (struct alignlist));
  1307.       if (l == NULL)
  1308.         {
  1309.           free (result);
  1310.           return NULL;
  1311.         }
  1312.     }
  1313.       l->exact = result;
  1314.       result = l->aligned = (char *) result + alignment - adj;
  1315.       l->next = _aligned_blocks;
  1316.       _aligned_blocks = l;
  1317.     }
  1318.  
  1319.   return result;
  1320. }
  1321. /* Allocate memory on a page boundary.
  1322.    Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  1323.  
  1324. This library is free software; you can redistribute it and/or
  1325. modify it under the terms of the GNU Library General Public License as
  1326. published by the Free Software Foundation; either version 2 of the
  1327. License, or (at your option) any later version.
  1328.  
  1329. This library is distributed in the hope that it will be useful,
  1330. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1331. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  1332. Library General Public License for more details.
  1333.  
  1334. You should have received a copy of the GNU Library General Public
  1335. License along with this library; see the file COPYING.LIB.  If
  1336. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  1337. Cambridge, MA 02139, USA.
  1338.  
  1339.    The author may be reached (Email) at the address mike@ai.mit.edu,
  1340.    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  1341.  
  1342. #ifndef    _MALLOC_INTERNAL
  1343. #define    _MALLOC_INTERNAL
  1344. #include <malloc.h>
  1345. #endif
  1346.  
  1347. #ifdef    __GNU_LIBRARY__
  1348. extern size_t __getpagesize __P ((void));
  1349. #else
  1350. #include "getpagesize.h"
  1351. #define     __getpagesize()    getpagesize()
  1352. #endif
  1353.  
  1354. static size_t pagesize;
  1355.  
  1356. __ptr_t
  1357. valloc (size)
  1358.      size_t size;
  1359. {
  1360.   if (pagesize == 0)
  1361.     pagesize = __getpagesize ();
  1362.  
  1363.   return memalign (pagesize, size);
  1364. }
  1365.